package com.smart.seckilldemo.commons.exception;



import com.smart.seckilldemo.commons.result.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.BindException;
import java.sql.SQLException;

/**
 * 全局捕捉异常
 * @author mtl
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> handler(Exception exception) {
        // 判断异常类型
        if (exception instanceof ServiceException) {
            //状态码
            return ResponseEntity.error(StatusCode.SERVER_ERROR);
        } else if(exception instanceof SQLException){
            return ResponseEntity.error(StatusCode.SERVER_ERROR);
        }else if(exception instanceof BindException){
            return ResponseEntity.error(StatusCode.PARAMETERS_ERROR);
        }else {
            log.info("错误："+exception.getMessage());
            return ResponseEntity.error(StatusCode.ERROR);
        }
    }
}
